package com.atzly.community.controller;

import com.atzly.community.constant.CommentConstant;
import com.atzly.community.entity.DiscussPost;
import com.atzly.community.entity.Page;
import com.atzly.community.service.ElastaticSearchService;
import com.atzly.community.service.LikeService;
import com.atzly.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

/**
 * 从es中进行搜索功能
 *
 * @author zlysimida
 * @date 2021/9/2 - 9:19
 */
@Controller
public class SearchContorller implements CommentConstant {

    @Autowired
    private ElastaticSearchService elastaticSearchService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;


    // /search?keyword
//    @GetMapping("/search")
//    public String search(String keyword, Page page, Model model) {
//
//        Map<String, Object> data = elastaticSearchService.searchFromEs(keyword, page.getOffset(), page.getLimit());
//        List<DiscussPost> discussPosts = null;
//        if (data.containsKey("page")) {
//            discussPosts = (List<DiscussPost>) data.get("page");
//        }
//        List<Map<String, Object>> mapList = new ArrayList<>();
//        if (discussPosts != null) {
//            for (DiscussPost post : discussPosts) {
//                Map<String, Object> map = new HashMap<>();
//                // 帖子
//                map.put("post", post);
//                // 作者
//                map.put("user", userService.selectById(post.getUserId()));
//                // 点赞数量
//                map.put("likeCount", likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId()));
//                mapList.add(map);
//            }
//        }
//        // 聚合数据
//        model.addAttribute("discussPosts", mapList);
//        // 回显关键字
//        model.addAttribute("keyword", keyword);
//
//        // 处理分页信息
//        page.setLimit(10);
//        page.setPath("/search?keyword=" + keyword);
//        if (data.containsKey("hitCounts")) {
//            page.setTotal(((Long) data.get("hitCounts")).intValue());
//        }
//
//        return "/site/search";
//    }


    // search?keyword=xxx
    @RequestMapping(path = "/search", method = RequestMethod.GET)
    public String search(String keyword, Page page, Model model) {
        // 搜索帖子
        org.springframework.data.domain.Page<DiscussPost> searchResult =
                elastaticSearchService.searchDiscussPost(keyword, page.getCurrent() - 1, page.getLimit());
        // 聚合数据
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (searchResult != null) {
            for (DiscussPost post : searchResult) {
                Map<String, Object> map = new HashMap<>();
                // 帖子
                map.put("post", post);
                // 作者
                map.put("user", userService.selectById(post.getUserId()));
                // 点赞数量
                map.put("likeCount", likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId()));

                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("keyword", keyword);

        // 分页信息
        page.setPath("/search?keyword=" + keyword);
        page.setTotal(searchResult == null ? 0 : (int) searchResult.getTotalElements());
        return "/site/search";
    }
}
